﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;

namespace DingChiServiceApp
{
    public class HaulBillDao : SqlServerDao<HaulBill>, IHaulBillService
    {
        private readonly string tableName = "tbHaulBill";
        public override string GetTableName()
        {
            return tableName;
        }
        [Obsolete("被 SelectNoUps(int rows) 代替", true)]
        public List<HaulBill> SelectNeenUps(int rows, string upTime)
        {
            string sql = $"select  top({rows}) *  from {tableName} where createTime >= '{upTime}' or modifyTime >= '{upTime}' order by createTime asc;";
            var datas = Instance.Select<HaulBill>(sql);
            return datas;
        }

        public List<HaulBill> SelectNoUps(int rows, string startDate = "")
        {
            string sql;
            if (string.IsNullOrEmpty(startDate))
            {
                sql = $"select  top({rows}) *  from {tableName} where isUpGovernment = 0 and sendNetWeight > 0 and receiveNetWeight > 0  order by createTime asc;";
            }
            else
            {
                sql = $"select  top({rows}) *  from {tableName} where isUpGovernment = 0 and sendNetWeight > 0 and receiveNetWeight > 0 and createTime >= '{startDate}'  order by createTime asc;";
            }
            var datas = Instance.Select<HaulBill>(sql);
            return datas;
        }

        public int UpdateUpGoverment(HaulBill bill)
        {
            string sql = $"UPDATE  {tableName} set isUpGovernment = 1 where id ={bill.id}";
            int res = Instance.Update(sql);
            return res;
        }

        public List<HaulBill> SelectNeedRepetitionUploads(int rows, string lastTime)
        {
            string sql = $"select  top({rows}) *  from {tableName} where isUpGovernment = 1 and sendNetWeight > 0 and receiveNetWeight > 0 and (createTime >= '{lastTime}' or modifyTime >= '{lastTime}')   order by createTime asc;";
            var datas = Instance.Select<HaulBill>(sql);
            return datas;
        }

        public Int32 CountIsUp()
        {
            string sql = $"select  *  from {tableName} where isUpGovernment = 1  ;";
            var datas = Instance.Select<HaulBill>(sql);
            return datas?.Count??0;
        }
    }
}
